package com.toune.movie.mvp.model;

import android.util.Log;

import com.toune.movie.base.Api;
import com.toune.movie.mvp.contract.ComicVideoContract;
import com.toune.movie.mvp.presenter.ComicVideoPresenter;
import com.toune.movie.vo.VarietyVo;
import com.vondear.rxtool.RxThreadPoolTool;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/15.
 */

public class ComicVideoModel implements ComicVideoContract.Model {
    ComicVideoPresenter tvVideoPresenter;

    public ComicVideoModel(ComicVideoPresenter tvVideoPresenter) {
        this.tvVideoPresenter = tvVideoPresenter;
    }

    private void paseHtml(String url) {
        try {
            //还是一样先从一个URL加载一个Document对象。
            Document doc = Jsoup.connect(url).get();
            //获取头部分类
            Elements fenlei = doc.getElementsByClass("fenlei");
            Elements as = fenlei.get(0).getElementsByTag("a");
            List<VarietyVo> headerVos = new ArrayList<>();
            for (int i = 0; i < as.size(); i++) {
                VarietyVo varietyVo = new VarietyVo();
                Element element = as.get(i);
                String detailUrl = Api.ComicUrl52 + element.attr("href");
                String text = element.text();
                varietyVo.setTitle(text);
                varietyVo.setDetailUrl(detailUrl);
                headerVos.add(varietyVo);
            }
            tvVideoPresenter.setHeaderVos(headerVos);

            Elements contents = doc.getElementsByClass("s-tab-main");
            Elements lis = contents.get(0).getElementsByTag("li");
            List<VarietyVo> varietyVoList = new ArrayList<>();
            for (int i = 0; i < lis.size(); i++) {
                //一个li
                Element element = lis.get(i);
                Elements a = element.getElementsByTag("a");
                String detailUrl = Api.BaseUrl52 + a.get(0).attr("href");//链接
                String title = a.get(0).attr("title");//标题

                Elements img = element.getElementsByTag("img");
                String imgUrl = img.get(0).attr("src");//图片

                Elements star = element.getElementsByClass("star");
                String detailText = star.get(0).text();

                VarietyVo varietyVo = new VarietyVo();
                varietyVo.setImagSrc(imgUrl);
                varietyVo.setDetailUrl(detailUrl);
                varietyVo.setTitle(title);
                varietyVo.setPingfen("");
                varietyVo.setDetaiText(detailText);
                varietyVoList.add(varietyVo);
            }
            tvVideoPresenter.setVideoList(varietyVoList);
        } catch (Exception e) {
            Log.i("mytag", e.toString());
        }
    }

    @Override
    public void getVideoList(final String url) {
        new RxThreadPoolTool(RxThreadPoolTool.Type.CachedThread, 1).execute(new Runnable() {
            @Override
            public void run() {
                paseHtml(url);
            }
        });
    }
}
